package net.zwj.hadoop.io;

import java.io.IOException;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.MapFile;
import org.apache.hadoop.io.SequenceFile;

/**
 * Re-creating the index for a MapFile
 * 
 * @author zwj
 * 
 */
public class MapFileFixer {

	public static void main(String[] args) throws Exception {
		// String uri = args[0];
		String mapUri = "/sequenceFileDemo";
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(URI.create(mapUri), conf, "hadoop");
		Path map = new Path(mapUri);
		Path mapData = new Path(map, MapFile.DATA_FILE_NAME);

		// Get key and value types from data sequence file
		SequenceFile.Reader reader = new SequenceFile.Reader(fs, mapData, conf);
		Class keyClass = reader.getKeyClass();
		Class valueClass = reader.getValueClass();
		reader.close();
		// Create the map file index file
		long entries = MapFile.fix(fs, map, keyClass, valueClass, false, conf);
		System.out.printf("Created MapFile %s with %d entries\n", map, entries);
	}
}
